/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhaojiu.service.impl;

import me.zhengjie.modules.zhaojiu.domain.Recommend;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhaojiu.repository.RecommendRepository;
import me.zhengjie.modules.zhaojiu.service.RecommendService;
import me.zhengjie.modules.zhaojiu.service.dto.RecommendDto;
import me.zhengjie.modules.zhaojiu.service.dto.RecommendQueryCriteria;
import me.zhengjie.modules.zhaojiu.service.mapstruct.RecommendMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2021-12-09
**/
@Service
@RequiredArgsConstructor
public class RecommendServiceImpl implements RecommendService {

    private final RecommendRepository recommendRepository;
    private final RecommendMapper recommendMapper;

    @Override
    public Map<String,Object> queryAll(RecommendQueryCriteria criteria, Pageable pageable){
        Page<Recommend> page = recommendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recommendMapper::toDto));
    }

    @Override
    public List<RecommendDto> queryAll(RecommendQueryCriteria criteria){
        return recommendMapper.toDto(recommendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecommendDto findById(Long recommendId) {
        Recommend recommend = recommendRepository.findById(recommendId).orElseGet(Recommend::new);
        ValidationUtil.isNull(recommend.getRecommendId(),"Recommend","recommendId",recommendId);
        return recommendMapper.toDto(recommend);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecommendDto create(Recommend resources) {
        return recommendMapper.toDto(recommendRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Recommend resources) {
        Recommend recommend = recommendRepository.findById(resources.getRecommendId()).orElseGet(Recommend::new);
        ValidationUtil.isNull( recommend.getRecommendId(),"Recommend","id",resources.getRecommendId());
        recommend.copy(resources);
        recommendRepository.save(recommend);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long recommendId : ids) {
            recommendRepository.deleteById(recommendId);
        }
    }

    @Override
    public void download(List<RecommendDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecommendDto recommend : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("老师ID", recommend.getTeacherId());
            map.put("学生ID", recommend.getStudentId());
            map.put("公司ID", recommend.getCompanyId());
            map.put("职位ID", recommend.getJobId());
            map.put("推荐时间", recommend.getDate());
            map.put("推荐附加信息", recommend.getContent());
            map.put("状态", recommend.getEnabled());
            map.put("创建者", recommend.getCreateBy());
            map.put("更新者", recommend.getUpdateBy());
            map.put("创建日期", recommend.getCreateTime());
            map.put("更新时间", recommend.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
